package com.hjs.servlet.user;

import com.hjs.bean.Department;
import com.hjs.bean.RightGrade;
import com.hjs.bean.Users;
import com.hjs.mapper.UsersMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author flamemaster
 * @date 2020/6/17
 **/
@WebServlet("/user/add")
public class Add extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        switch (Integer.parseInt(request.getParameter("addType"))) {
            case 1:
                addUser(requestUtil);
                break;
            case 2:
                addDepartment(requestUtil);
                break;
            case 3:
                addGrade(requestUtil);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + Integer.parseInt(request.getParameter("addType")));
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    private void addUser(RequestUtil requestUtil) {
        Users users = new Users(requestUtil.getString("userID"), requestUtil.getString("userName"),
                requestUtil.getString("userPassword"), requestUtil.getInt("departmentID"),
                requestUtil.getInt("rightGradeID"));
        Mapper<UsersMapper> usersMapperMapper = new Mapper<>(UsersMapper.class);
        try {
            UsersMapper usersMapper = usersMapperMapper.getMapper();
            boolean result = usersMapper.addOneUsers(users);
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            usersMapperMapper.closeSession();
        }
    }

    private void addDepartment(RequestUtil requestUtil) {
        Department department = new Department(null,
                requestUtil.getInt("superDepartmentID"), requestUtil.getString("departmentName"));
        Mapper<UsersMapper> usersMapperMapper = new Mapper<>(UsersMapper.class);
        try {
            UsersMapper usersMapper = usersMapperMapper.getMapper();
            boolean result = usersMapper.addOneDepartment(department);
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            usersMapperMapper.closeSession();
        }
    }

    private void addGrade(RequestUtil requestUtil) {
        RightGrade rightGrade = new RightGrade(requestUtil.getInt("rightGradeID"), requestUtil.getString("rightGradeName"));
        Mapper<UsersMapper> usersMapperMapper = new Mapper<>(UsersMapper.class);
        try {
            UsersMapper usersMapper = usersMapperMapper.getMapper();
            boolean result = usersMapper.addOneRightGrade(rightGrade);
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            requestUtil.responseWrite("{\"result\":false}");
        }finally {
            usersMapperMapper.closeSession();
        }
    }
}

